package com.tsunami.auth.service.auth.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsunami.auth.mapper.MenuMapper;
import com.tsunami.auth.service.auth.IMenuService;
import com.tsunami.auth.service.auth.IUserService;
import com.tsunami.authority.dto.auth.MenuUpdateDTO;
import com.tsunami.authority.entity.auth.Menu;
import com.tsunami.base.R;
import com.tsunami.user.model.SysUser;
import com.tsunami.utils.PageUtils;
import com.tsunami.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author lx
 * @since 2021-09-23
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private IUserService userService;

    @Override
    public R selectPageList(Menu data) {
        Page<Menu> pages = baseMapper.selectPageList(PageUtils.buildPage(),data);
        return R.ok().data(pages);
    }

    @Override
    public Menu updateMenu(SysUser loginUser,MenuUpdateDTO data) {
        Menu menu = getById(data.getId());
//        menu.setTenantId();
        if(StringUtils.isNotEmpty(data.getName())){
            menu.setName(data.getName());
        }
        if(StringUtils.isNotEmpty(data.getDescribe())){
            menu.setDescribe(data.getDescribe());
        }
        if(data.getSPublic()!= null){
            menu.setSPublic(data.getSPublic());
        }
        if(StringUtils.isNotEmpty(data.getPath())){
            menu.setPath(data.getPath());
        }
        if(StringUtils.isNotEmpty(data.getComponent())){
            menu.setComponent(data.getComponent());
        }
        if(data.getSEnable()!= null){
            menu.setSEnable(data.getSEnable());
        }
        if(data.getSortValue()!= null){
            menu.setSortValue(data.getSortValue());
        }
        if(StringUtils.isNotEmpty(data.getIcon())){
            menu.setIcon(data.getIcon());
        }
        if(StringUtils.isNotEmpty(data.getGroup())){
            menu.setGroup(data.getGroup());
        }
        if(data.getParentId()!=null){
            menu.setParentId(data.getParentId());
        }
        menu.setUpdateUser(loginUser.getId());
        super.updateById(menu);
        return menu;
    }

    @Override
    public List<Menu> findVisibleMenu(String group, Long userId,String code) {
        List<Menu> visibleMenu = baseMapper.findVisibleMenu(group,userId,code);
        return visibleMenu;
    }
}
